package com.example.tianling.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.common.vo.article;
import com.example.tianling.dao.BookMapper;
import com.example.tianling.dao.MaterialMapper;
import com.example.tianling.entity.ArticleEntity;
import com.example.tianling.entity.BookEntity;
import com.example.tianling.entity.MaterialEntity;
import com.example.tianling.entity.UserEntity;
import com.example.tianling.service.BookService;
import com.example.tianling.service.MaterialService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MaterialServiceImpl
        extends ServiceImpl<MaterialMapper, MaterialEntity>
        implements MaterialService {
    @Autowired
    MaterialMapper materialMapper;
    @Override
    public List<MaterialEntity> getMaterial(String region, String type, int pageNum,String keyword) {
        Page<MaterialEntity> pg = new Page<>(pageNum, 10);
        IPage<MaterialEntity> articleIPage = materialMapper.selectJoinPage(pg,MaterialEntity.class,
                new MPJLambdaWrapper<MaterialEntity>()
                        .select(MaterialEntity::getId)
                        .select(MaterialEntity::getTitle)
                        .select(MaterialEntity::getIsNeedVip)
                        .select(MaterialEntity::getContent)
                        .select(MaterialEntity::getMaterialType)
                        .likeLeft(MaterialEntity::getMaterialType,type)
                        .like(MaterialEntity::getRegion,region)
                        .like(MaterialEntity::getTitle,keyword));
        for(MaterialEntity materialEntity :articleIPage.getRecords()){
            materialEntity.setContent(materialEntity.getContent().substring(0,20));
        }
        return articleIPage.getRecords();
    }

    @Override
    public List<MaterialEntity> searchMaterial(int pageNum,String keyword) {
        Page<MaterialEntity> pg = new Page<>(pageNum, 10);
        IPage<MaterialEntity> articleIPage = materialMapper.selectJoinPage(pg,MaterialEntity.class,
                new MPJLambdaWrapper<MaterialEntity>()
                        .select(MaterialEntity::getId)
                        .select(MaterialEntity::getTitle)
                        .select(MaterialEntity::getIsNeedVip)
                        .select(MaterialEntity::getContent)
                        .like(MaterialEntity::getTitle,keyword));
        for(MaterialEntity materialEntity :articleIPage.getRecords()){
            materialEntity.setContent(materialEntity.getContent().substring(0,20));
        }
        return articleIPage.getRecords();
    }

    @Override
    public List<String> getMaterialsType(String region) {
        QueryWrapper<MaterialEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct material_type");
        queryWrapper.like("region",region);
        List<MaterialEntity> materialEntities= materialMapper.selectList(queryWrapper);
        List<String> materialTypes = materialEntities.stream()
                .map(MaterialEntity::getMaterialType)
                .collect(Collectors.toList());
        return materialTypes;
    }
     @Override
        public MaterialEntity getMaterialsDetail(Integer id) {
            QueryWrapper<MaterialEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("material_type","id","region","title","content","create_time","is_need_vip","background","influence","method","keyword");            queryWrapper.eq("id",id);
            return materialMapper.selectOne(queryWrapper);
        }
    @Override
        public String getAnalysis(Integer id){
            QueryWrapper<MaterialEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("analysis");
            queryWrapper.eq("id",id);
            MaterialEntity materialEntity = materialMapper.selectOne(queryWrapper);
            if(materialEntity==null){
                return "";
            }
            return materialEntity.getAnalysis();
        }

    @Override
    public MaterialEntity nextMaterial(String region, String type, int id,String keyword){
        MaterialEntity materialEntity = this.lambdaQuery()
                .select(MaterialEntity::getId,MaterialEntity::getRegion,MaterialEntity::getMaterialType,MaterialEntity::getTitle,MaterialEntity::getContent,MaterialEntity::getIsNeedVip)
                .like(MaterialEntity::getRegion, region)
                .like(MaterialEntity::getMaterialType, type)
                .like(MaterialEntity::getTitle,keyword)
                .orderByDesc(MaterialEntity::getId)
                .gt(MaterialEntity::getId, id)  // 添加 book_id < 11 的条件
                .last("LIMIT 1")
                .one();
        return materialEntity;
    }

    @Override
    public MaterialEntity lastMaterial(String region, String type, int id,String keyword){
        MaterialEntity materialEntity = this.lambdaQuery()
                .select(MaterialEntity::getId,MaterialEntity::getRegion,MaterialEntity::getMaterialType,MaterialEntity::getTitle,MaterialEntity::getContent,MaterialEntity::getIsNeedVip)
                .like(MaterialEntity::getRegion, region)
                .like(MaterialEntity::getMaterialType, type)
                .like(MaterialEntity::getTitle,keyword)
                .orderByDesc(MaterialEntity::getId)
                .lt(MaterialEntity::getId, id)  // 添加 book_id < 11 的条件
                .last("LIMIT 1")
                .one();
        return materialEntity;
    }
}
